<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Produk extends CI_Controller {
	private $limit=10;
	
	function __construct(){
		parent::__construct();
		
		$index	= $this->config->item('index_page');
		$host	= $this->config->item('base_url');

		$this->url = empty($index) ? $host : $host . $index . '/';
		$this->user	= unserialize(base64_decode($this->session->userdata('user')));	
		$this->load->model('mProduk');
		
		//get session.. 'user' tuh nama session yang udah di set 
//		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
	}
	
	public function index($offset=0,$order_column='id_barang', $order_type='asc'){
	if (!empty($this->user)){
		if(empty($offset)) $offset=0;
		if(empty($order_column)) $order_column='id_barang';
		if(empty($order_type)) $order_type='asc';
		//todo : check for valid_barang column
		
		//load data siswa
		$produks=$this->mProduk->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
		//generate pagination
		$config['base_url']=site_url('admin/produk/index/');
		$config['total_rows']=$this->mProduk->count_all();
		$config['per_page']=$this->limit;
		$config['uri_segment']=3;
		$this->pagination->initialize($config);
		$data['pagination']=$this->pagination->create_links();
		
		//generate table data
		$this->table->set_empty("&nbsp;");
		$new_order=($order_type=='asc'?'desc':'asc');
		$this->table->set_heading(
			'ID BARANG','ID KATEGORI','ID MEREK','NAMA BARANG','HARGA','STOK','DESKRIPSI','URL GAMBAR','ACTIONS');
		foreach($produks as $siswa){
				$this->table->add_row($siswa->ID_BARANG,$siswa->ID_KAT,$siswa->ID_MEREK,$siswa->NAMA_BRG,$siswa->HARGA,$siswa->STOK,$siswa->DESKRIPSI,$siswa->URL_GAMBAR,anchor('admin/produk/update/'.$siswa->ID_BARANG,'update',array('class'=>'update')).' '.anchor('admin/produk/delete/'.$siswa->ID_BARANG,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data siswa?')"))
				);
		}
			
			$data['table']=$this->table->generate();
			
			if($this->uri->segment(3)=='delete success') $data['message']='Data berhasil dihapus';
			else if ($this->uri->segment(3)=='add_success') $data['message']='Data berhasil ditambah';
			else $data['message']='';
			//load view
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/produk/produk', $data, true);

		$this->load->view('index', $data);
		}
	   else
	   {
		 //If no session, redirect to login page
		 redirect('admin/login');
	   }
	}
		
	function add() {
		//set common properties
		$data['title']='tambah barang baru';
		$data['action']=site_url('admin/produk/add');
		$data['link_back']=anchor('admin/produk/index/','Back to list of produk',array('class'=>'back'));
		
		$this->_set_rules();
		
		//run valid_barangation
		if($this->form_validation->run()===FALSE){
			$data['message']='';
			//set common properties
			$data['title']='Add new produk';
			$data['message']='';
			$data['produk']['ID_BARANG']='';
			$data['produk']['ID_KAT']='';
			$data['produk']['ID_MEREK']='';
			$data['produk']['NAMA_BRG']='';
			$data['produk']['HARGA']='';
			$data['produk']['STOK']='';
			$data['produk']['DESKRIPSI']='';
			$data['produk']['URL_GAMBAR']='';
			$data['link_back']=anchor('admin/produk/index','Lihat daftar barang',array('class'=>'back'));
			$data['url'] = $this->url;
			$data['header'] = $this->load->view('admin/header/header', $data, true);
			$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
			$data['content'] = $this->load->view('admin/rcontent/produk/produkEdit', $data, true);

			$this->load->view('index',$data);
		} else{
			//save data
			$produk=array('id_kat'=>$this->input->post('id_kat'),'id_merek'=>$this->input->post('id_merek'),'nama_brg'=>$this->input->post('nama_brg'),'harga'=>$this->input->post('harga'),'stok'=>$this->input->post('stok'),'deskripsi'=>$this->input->post('deskripsi'),'url_gambar'=>$this->input->post('url_gambar'));
			$id_barang=$this->mProduk->save($produk);
			
			//set form input nama_brg="id_barang"
			$this->validation->id_barang=$id_barang;
			
			redirect('admin/produk/index/add_success');
		}
	}
	
	// function view($id_barang){
		// set common properties
		// $data['title']='siswa Details';
		// $data['link_back']=anchor('siswa/index/','Lihat daftar siswas',array('class'=>'back'));
		
		// get siswa details
		// $data['siswa']=$this->siswa_model->get_by_id($id_barang)->row();
		
		// $param['url'] = $this->url;
		// $data['header'] = $this->load->view('admin/header/header', $param, true);
		// $data['menu'] = $this->load->view('admin/menu/menu', $param, true);
		// $data['content'] = $this->load->view('admin/rcontent/produk/produkView', $param, true);

		// $this->load->view('index',$data);
		// load view
	// }
	
	function update($id_barang){
		//set common properties
		$data['title']='Update barang';
		
		//set validation properties
			$this->_set_rules();
			$data['action']=('admin/produk/update/'.$id_barang);
			
			//run validation
			if($this->form_validation->run()===FALSE){
				$data['message']='';
				$data['produk']=$this->mProduk->get_by_id($id_barang)->row_array();
				
				//set common properties
				$data['title']='Update Produk';
				$data['message']='';
			}else {
				//save data
				$id_barang=$this->input->post('id_barang');
				$produk=array('id_kat'=>$this->input->post('id_kat'),'id_merek'=>$this->input->post('id_merek'),'nama_brg'=>$this->input->post('nama_brg'),'harga'=>$this->input->post('harga'),'stok'=>$this->input->post('stok'),'deskripsi'=>$this->input->post('deskripsi'),'url_gambar'=>$this->input->post('url_gambar'));
				$this->mProduk->update($id_barang,$produk);
				$data['produk']=$this->mProduk->get_by_id($id_barang)->row_array();
				
				//set user message
				$data['message']='update barang success';
			}
			$data['link_back']=anchor('admin/produk/index/','Lihat daftar barang',array('class'=>'back'));
			//load view
			$data['url'] = $this->url;
			$data['header'] = $this->load->view('admin/header/header', $data, true);
			$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
			$data['content'] = $this->load->view('admin/rcontent/produk/produkEdit', $data, true);

			$this->load->view('index',$data);
	}
	
	function delete($id_barang){
		//delete siswa
		$this->mProduk->delete($id_barang);
		//redirect to siswa list page
		redirect('admin/produk/index/delete_success','refresh');
	}
	
	//valid_barangation rules
	function _set_rules(){
		$this->form_validation->set_rules('nama_brg','nama_brg','required|trim');
		$this->form_validation->set_rules('deskripsi','deskripsi','required');
	}
	
	//date_valid_barangation callback
	function valid_date($str)
	{
		if(!preg_match('#[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])#',$str))
		{
			$this->form_valid_barangation->set_message('valid_barang_date','date format is not valid_barang. yyyy-mm-dd');
			return false;
		}
		else
		{
			return true;
		}
	}	
}